<?php
include "../../../lib/list.php";
if ($_GET['s_no']){
	$s_no = $_GET['s_no'];
	$sear_char .= " AND stre.s_no = $s_no";
}

if ($_POST['s_no']){
	$s_no = $_POST['s_no'];
	$sear_char .= " AND stre.s_no = $s_no";
}
if ($_GET['sg_no'])$sg_no = $_GET['sg_no'];
if ($_POST['sg_no'])$sg_no = $_POST['sg_no'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<head>
<link rel="stylesheet" href="../../../css/list.css" type="text/css"></link>
<link rel="stylesheet" href="../../../css/insert.css" type="text/css"></link>
<link rel="stylesheet" href="../../../css/table.sorter.css" type="text/css" media="print, projection, screen" />
<link type="text/css" rel="stylesheet" href="../../../css/dhtmlgoodies_calendar.css?random=20051112" media="screen"></link>

<script type="text/javascript" src="../../../js/dhtmlgoodies_calendar.js?random=20060118"></script>
<script type="text/javascript" src="../../../js/list.js"></script>
<script type="text/javascript" src="../../../js/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../../js/jquery.tablesorter.js"></script>
<script type="text/javascript">
function page_move(page){
			document.view.page.value = page;
			document.view.target = 'right';
			document.view.action = 'view.php';
			document.view.method = 'post';
			document.view.submit();
}

$(function(){
	$("select[name='sg_no']").change(function(){
	    var sg_no = $("select[name='sg_no']").attr('value');
	    $.get('../../../lib/select.php?sg_no='+sg_no,function(res){
	        $("select[name='s_no']").html(res);
	    });
	});
});


function movepage(url,get,value){
	var sg_no = document.getElementsByName('sg_no')[0].value;
	location.href = url+"?"+get+"="+value+"&sg_no="+sg_no;
}


	</script>
</head>
<body>
	<div style="width: 1024px;">
		<div
			style="border-bottom: 1px solid #e8e8e8; text-align: left; font-size: 12px; margin-bottom: 12px; color: #9a9a9a">:::
			재고 관리&nbsp;⇒&nbsp;재고 관리표</div>
			<div style="float: left; padding: 5px; font-size:11px;">
						<select name='sg_no' class="default">
						<option value="-3">:: 점포그룹 선택 ::</option>
						<?
						$qry = "SELECT * FROM uatp_shopgroup order by no DESC";
						$query = DBquery($qry);
						while($list = mysql_fetch_array($query)){
							?>
						<option value="<?=$list['no']?>"<?if($list['no']==$sg_no)echo " selected";?>><?=$list['name']?></option>
						<?
						}
						mysql_close();
						?>
						</select>
						
						<select name='s_no' class="default" onChange="movepage('stockreview.php','s_no',this.value);";>
							<option value="">:: 점포 선택 ::</option>
							<?
							if($sg_no){
								$qry = "SELECT * FROM uatp_shopinfo WHERE sg_no = $sg_no order by no DESC";
								$query = DBquery($qry);
								while($list = mysql_fetch_array($query)){
									?>
														<option value="<?=$list['no']?>"<?if($list['no']==$s_no)echo " selected";?>><?=$list['name']?></option>
														<?
														}
														mysql_close();
							}
							?>
						</select>
			</div>
			<form name="detail" method="post" action="query.php" enctype=multipart/form-data>
						<input type="hidden" name="page" value=""/>
						<input type="hidden" value="<?=$s_no?>" name="s_no" />
						<input type="hidden" value="<?=$sg_no?>" name="sg_no" />
			<table cellspacing="1" class="tablesorter">
			<thead>
				<tr>
								<th>점포명</th>
								<th>지난재고파악일</th>
								<th>지난재고파악수</th>
								<th>이번재고파악일</th>
								<th>barcode</th>
								<th>상품명</th>
								<th>입고</th>
								<th>결함품</th>
								<th>이동수</th>
								<th>판매수</th>
								<th>매상금액</th>
								<th>누계입고수</th>
								<th>누계결함품수</th>
								<th>누계이동수</th>
								<th>누계판매수</th>
								<th>현재재고수</th>
								</tr>
								<tr>
				</tr>
			</thead>
			<?php 
			$page=$_POST['page'];
			
			$sql = "SELECT stre.`s_no` , stre.`p_no` , (`total`.allin +  `total`.alde +  `total`.almo +  `total`.alsa) AS theory
							, stre.`realstock` , stre.instock, stre.defect, stre.move, pr.productname
							, pr.barcode, stre.`date` , stre.`lastdate` ,  `total`.allin,  `total`.alde, uatp_stock.count
							, `total`.almo,  `total`.alsa,  `total`.algap, stre2.realstock AS laststock, sh.name AS shopname

					FROM  `uatp_stockreview` as stre
						INNER JOIN uatp_product as pr ON stre.p_no = pr.no
						INNER JOIN uatp_shopinfo as sh ON stre.s_no = sh.no
						INNER JOIN uatp_stock ON stre.p_no = uatp_stock.p_no AND stre.s_no = uatp_stock.s_no
						
						LEFT OUTER JOIN (SELECT  `s_no`, `p_no`, SUM(`instock`) AS `allin`, SUM(`defect`) AS `alde`
						, SUM(`move`) AS  `almo`, SUM(`sale`) AS  `alsa` 
						, SUM(`gap`) AS  `algap` FROM  uatp_stockreview as stre GROUP BY s_no, p_no) AS total ON stre.s_no = total.s_no AND stre.p_no = total.p_no
						
						LEFT OUTER JOIN uatp_stockreview as stre2 on stre.p_no = stre2.p_no and stre.s_no = stre2.s_no and stre.lastdate = stre2.date
					
					
					WHERE  stre.`date` IN (SELECT MAX(stre3.DATE) FROM  uatp_stockreview as stre3 GROUP BY  `s_no`, `p_no`)
					$sear_char GROUP BY  `s_no`,  `p_no`";
			
			$total = DBnumrows($sql);
			
			$scale=15;
			if ($page == ''){
				$page=1;
			}
			
			$cpage = intval($page);//현재 페이지
			$totalpage = intval($total/$scale); //전체 페이지 갯수
			if ($totalpage*$scale != $total)
			$totalpage = $totalpage + 1;
			
			if ($cpage ==1) {
				$cline = 0 ;
			} else {
				$cline = ($cpage*$scale) - $scale ;
			}
			
			$limit=$cline+$scale;
			
			if ($limit >= $total)
			$limit=$total;
			
			$scale1 = $limit - $cline;
			
			$query = $sql." DESC LIMIT $cline, $scale1";
			$total = DBnumrows($query);
			$result = DBquery($query);
			
			for($i=1; $list = mysql_fetch_array($result); $i++){
												
				?>
			<tbody>
			<tr>
				<td><?=$list['shopname']?></td>
				<td><?if(strtotime($list['lastdate'])=='0')echo "지난재고조사없음"; else echo date("Y-m-d",strtotime($list['lastdate']));?></td>
				<td><?=$list['laststock']?></td>
				<td><?=date("Y-m-d",strtotime($list['date']))?></td>
				<td><?=$list['barcode']?></td>
				<td><?=$list['productname']?></td>
				<td><?=$list['instock']?></td>
				<td><?=$list['defect']?></td>
				<td><?=$list['move']?></td>
				<td>판매수</td>
				<td>매상금액</td>
				<td><?=$list['allin']?></td>
				<td><?=$list['alde']?></td>
				<td><?=$list['almo']?></td>
				<td>누계판매수</td>
				<td><?=$list['count']?></td>
			</tr>
			</tbody>
			<?php 
				}
				mysql_free_result($result);
			?>
		</table>
		<div style="text-align:left;float:left;padding:0;margin:0;width:700px;">
		<?php page_avg($totalpage,$cpage);?>	
						</div>
			</form>
	</div>
</body>
</html>
